
package com.feib.soeasy.action;

import java.util.ArrayList;
import java.util.List;

import com.feib.soeasy.model.FormatDefinition;
import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.PayerTemp;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.PayerTempService;
import com.feib.soeasy.service.UploadFileService;
import com.feib.soeasy.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrors;


/**
 * @title (#)Soez2002ActionBean.java<br>
 * @description 繳款人資料上傳放行<br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/1/31 create by Jimmy Liu<br>
 */
public class Soez2002ActionBean extends BaseActionBean {

    private static final String DEFAULT_JSP = "/WEB-INF/views/soez2002/soez2002.jsp";

    /**
     * 繳款人整批上傳放行清單畫面
     */
    private static final String UPLOAD_LIST_JSP = "/WEB-INF/views/soez2002/soez2002List.jsp";

    /**
     * 繳款人放行結果畫面
     */
    private static final String AFTER_CONFIRM_JSP = "/WEB-INF/views/soez2002/afterConfirm.jsp";

    /**
     * 檔案內容
     */
    private static final String AJAX_QUERY_PAYMENT_JSP = "/WEB-INF/views/soez2002/payerTempList.jsp";

    /**
     * 查詢結果
     */
    private PaginatedList<UploadFile> queryResult;

    /**
     * 檔案放行結果
     */
    private String confirmMessage;

    /**
     * 繳款人明細查詢結果
     */
    private PaginatedList<PayerTemp> queryPayerTempResult;

    @SpringBean("uploadFileService")
    private UploadFileService uploadFileService;

    @SpringBean("payerTempService")
    private PayerTempService payerTempService;

    private UploadFile qryUploadFile;
    
    private List<UploadFile> checkUploadFile;

    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP); 
    }

    /**
     * 查詢待放行清單
     * @return
     */
    public Resolution doAjaxQuery()
    {
        logger.debug("Enter doAjaxQuery mehtod.");
        try {
            if (null == qryUploadFile)
                qryUploadFile = new UploadFile();

            // 查上傳人員與目前使用者同一個Group 的案件
            Group group = new Group();
            group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
            qryUploadFile.setGroup(group);

            //轉入類別 繳款人資料
            FormatDefinition formatDefinition = new FormatDefinition();
            formatDefinition.setFormatType(new Integer(2));
            qryUploadFile.setFormatDefinition(formatDefinition);
            
            if (null == queryResult)
                queryResult = new PaginatedList<UploadFile>();
            queryResult.setPageNumber(1);
            queryResult.setPageSize(-1);;
            
            //呼叫服務查詢
            queryResult.setPaginatedList(uploadFileService.queryUnConfirmByUploadFile(qryUploadFile));

        }catch (Exception e) {
            logger.error("查詢待放行清單錯誤(Soez2002ActionBean.doAjaxQuery):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doDefulat.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(UPLOAD_LIST_JSP);
    }


    /**
     * 帳單放行審核
     * fileStatus : 4: 確認放行, 5: 不放行(表上傳資料有誤)
     * isConfirmStatus : True: 放行 False: 不放行
     * confirmDate : 放行日期
     * @return
     */
    public Resolution doAjaxPayersConfirm()
    {
        logger.debug("Enter doAjaxPayersConfirm mehtod.");
        try {
            if (null == checkUploadFile)
                checkUploadFile = new ArrayList<UploadFile>();
                
            payerTempService.updatePayerStatusByUploadFile(checkUploadFile, qryUploadFile.getFileStatus());

            if (qryUploadFile.getFileStatus().equals(UploadFile.FILE_STATUS_CONFIRM_OK))
                confirmMessage = "檔案已放行！";
            else
                confirmMessage = "檔案已刪除！";

        } catch (Exception e) {
            confirmMessage = "處理失敗！";
            logger.error(e.toString(), e);
        }
        return new ForwardResolution(AFTER_CONFIRM_JSP);
    }


    /**
     * 查詢轉入的帳單資料
     * @return
     */
    public Resolution doAjaxQueryFilePayers()
    {
        logger.debug("Enter doAjaxQueryFilePayers mehtod.");
        try {

            // 設定查詢條件
            PayerTemp qryPayerTemp = new PayerTemp();
            //如果不是銀行端，則查詢條件要加入帳單業者限制
            if (!UserUtil.isBankGroup())
            {
                Group group = new Group();
                group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
                qryPayerTemp.setGroup(group);
            }
            UploadFile uploadFile = new UploadFile();
            uploadFile.setUploadFilePk(qryUploadFile.getUploadFilePk());
            qryPayerTemp.setUploadFile(uploadFile);
            //呼叫服務查詢
            queryPayerTempResult = payerTempService.queryByPayerTemp(qryPayerTemp, queryPayerTempResult);

        }
        catch (Exception e) {
            logger.error("繳款人內容查詢錯誤(Soez2002ActionBean.doAjaxQueryFilePayers):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(AJAX_QUERY_PAYMENT_JSP);
    }

    
    /**
     * @return the queryResult
     */
    public PaginatedList<UploadFile> getQueryResult() {
        return queryResult;
    }

    
    /**
     * @param queryResult the queryResult to set
     */
    public void setQueryResult(PaginatedList<UploadFile> queryResult) {
        this.queryResult = queryResult;
    }

    
    /**
     * @return the confirmMessage
     */
    public String getConfirmMessage() {
        return confirmMessage;
    }

    
    /**
     * @param confirmMessage the confirmMessage to set
     */
    public void setConfirmMessage(String confirmMessage) {
        this.confirmMessage = confirmMessage;
    }

    
    /**
     * @return the queryPayerTempResult
     */
    public PaginatedList<PayerTemp> getQueryPayerTempResult() {
        return queryPayerTempResult;
    }

    
    /**
     * @param queryPayerTempResult the queryPayerTempResult to set
     */
    public void setQueryPayerTempResult(PaginatedList<PayerTemp> queryPayerTempResult) {
        this.queryPayerTempResult = queryPayerTempResult;
    }

    
    /**
     * @return the qryUploadFile
     */
    public UploadFile getQryUploadFile() {
        return qryUploadFile;
    }

    
    /**
     * @param qryUploadFile the qryUploadFile to set
     */
    public void setQryUploadFile(UploadFile qryUploadFile) {
        this.qryUploadFile = qryUploadFile;
    }

    
    /**
     * @return the checkUploadFile
     */
    public List<UploadFile> getCheckUploadFile() {
        return checkUploadFile;
    }

    
    /**
     * @param checkUploadFile the checkUploadFile to set
     */
    public void setCheckUploadFile(List<UploadFile> checkUploadFile) {
        this.checkUploadFile = checkUploadFile;
    }

}
